/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgChangePassword.java
 *
 * Created on Aug 23, 2012, 10:21:57 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.MD5Passing;
import Processing.dbclass.Staff;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgChangePassword extends javax.swing.JDialog {

    /** Creates new form dlgChangePassword */
    public dlgChangePassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        pssConfirmPassWord = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        pssNewPassWord = new javax.swing.JPasswordField();
        pssOldPassWord = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change Password");
        setResizable(false);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setPreferredSize(new java.awt.Dimension(350, 300));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel2.setText("Change Password");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Confirm Password:");

        pssConfirmPassWord.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        pssConfirmPassWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pssConfirmPassWordActionPerformed(evt);
            }
        });

        btnLogin.setText("Ok");
        btnLogin.setToolTipText("");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("New Password:");

        pssNewPassWord.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        pssNewPassWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pssNewPassWordActionPerformed(evt);
            }
        });

        pssOldPassWord.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        pssOldPassWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pssOldPassWordActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Old Password:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(pssOldPassWord, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(jLabel5)
                    .addComponent(pssNewPassWord, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(pssConfirmPassWord, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pssOldPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pssNewPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pssConfirmPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pssConfirmPassWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pssConfirmPassWordActionPerformed
        // TODO add your handling code here:
        btnLogin.doClick();
}//GEN-LAST:event_pssConfirmPassWordActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
        MD5Passing md5 = new MD5Passing();

        if (db.login(con, username).compareTo(md5.makePass(pssOldPassWord.getText())) == 0) {
            String staffID = db.searchStaff(con, 6, username).getValueAt(0, 0).toString();

            Staff staff = db.generateStaff(con, staffID);

            if (pssNewPassWord.getText().length() >= 6) {
                if (pssNewPassWord.getText().compareTo(pssConfirmPassWord.getText()) == 0) {
                    staff.setPassword(md5.makePass(pssNewPassWord.getText()));

                    if (db.editStaff(con, staffID, staff)) {
                        JOptionPane.showMessageDialog(this, "Change password successful!");
                    } else {
                        JOptionPane.showMessageDialog(this, "Change password fail!");
                    }

                    this.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(this, "New Password and Confirm Password are not same!");
                }       
            } else {
                JOptionPane.showMessageDialog(this, "Password must more than 6 character!");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Wrong Password or Username!");
        }
}//GEN-LAST:event_btnLoginActionPerformed

    private void pssNewPassWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pssNewPassWordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pssNewPassWordActionPerformed

    private void pssOldPassWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pssOldPassWordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pssOldPassWordActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgChangePassword dialog = new dlgChangePassword(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private String username;

    public void setUsername(String username) {
        this.username = username;

        this.setTitle("Change Password - " + username);
    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField pssConfirmPassWord;
    private javax.swing.JPasswordField pssNewPassWord;
    private javax.swing.JPasswordField pssOldPassWord;
    // End of variables declaration//GEN-END:variables

}
